home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 14 / Hot Mix 14.iso / HTML / vendors / 3Name3D / TickerTape.java < prev    next >
Text File  |  1996-07-22  |  7KB  |  183 lines

  1. * TickerTape - Ticker tape program written in java
  2.  *          I guess this is pretty much becoming the "Hello World" program
  3.  *          for Java.  This is the basic TickerTape java program with the
  4.  *          added feature of selecting and entry and connectioning to an
  5.  *          associated URL.  There is much that can be added.
  6.  *  Patrick Palmer, 1/3/96
  7.  */
  8.  
  9. import java.applet.Applet;
  10. import java.awt.*;
  11. import java.lang.*;
  12. import java.net.*;
  13.  
  14.  
  15. public class TickerTape extends Applet implements Runnable {
  16.  
  17.         private final static int NUM_TEXT = 10;
  18.  
  19.         private int delay = 100;
  20.         private int offset = 0;
  21.         private int width;
  22.         private int twidth = 0;
  23.         private int height;
  24.         private String text = null;
  25.         private String[] url;
  26.         private int[] url_off;
  27.         private int url_count = 0;
  28.         private Thread thread = null;
  29.         private int baseline;
  30.         private Image image;
  31.         private Font font;
  32.         private int step = 3;
  33.  
  34.         public void init() {
  35.                 int fsize = 18;                         // default font size 
  36.                 String str, arg;
  37.                 int count;
  38.  
  39.                 setBackground(Color.black);
  40.                 setForeground(Color.white);
  41.  
  42.                 // get parameters
  43.                 if ((str = super.getParameter("size")) != null)
  44.                         fsize = Integer.valueOf(str).intValue();
  45.                 if ((str = super.getParameter("font")) == null)
  46.                         str = "TimesRoman";
  47.                 this.font = new Font(str, Font.PLAIN, fsize);
  48.                 if ((str = super.getParameter("delay")) != null)
  49.                         this.delay = Integer.valueOf(str).intValue();
  50.                 if ((str = super.getParameter("step")) != null)
  51.                         this.step = Integer.valueOf(str).intValue();
  52.                 if ((str = super.getParameter("count")) != null)
  53.                         count = Integer.valueOf(str).intValue();
  54.                 else
  55.                         count = NUM_TEXT;
  56.  
  57.                 // get height and width
  58.                 this.width = super.size().width;
  59.                 this.height = super.size().height;
  60.  
  61.                 // set up secondary image
  62.                 this.image = createImage(this.width, this.height);
  63.  
  64.                 // set up objects
  65.                 this.url = new String[count];
  66.                 this.url_off = new int[count];
  67.                 FontMetrics fm = image.getGraphics().getFontMetrics(this.font);
  68.                 this.baseline = fm.getHeight() - fm.getDescent();
  69.  
  70.                 // read in text lines
  71.                 for (int i = 0; i < count; i++) {
  72.                         arg = "text" + i;
  73.                         if ((str = super.getParameter(arg)) != null) {
  74.  
  75.                                 // save text
  76.                                 if (this.text == null)
  77.                                         this.text = str;
  78.                                 else
  79.                                         this.text = this.text + str;
  80.  
  81.                                 // get the pixel width of the string
  82.                                 this.twidth = fm.stringWidth(text);
  83.                                 this.url_off[this.url_count] = twidth;
  84.                                 
  85.                                 // get the url address
  86.                                 arg = "url" + i;
  87.                                 this.url[this.url_count] = super.getParameter(arg);
  88.  
  89.                                 this.url_count++;
  90.                         }
  91.                 }
  92.         } 
  93.  
  94.         public final synchronized void update(Graphics g) { 
  95.                 int off;
  96.  
  97.                 // make sure there is text
  98.                 if (this.text == null)
  99.                         return;
  100.                 // draw
  101.                 Graphics gfx = this.image.getGraphics();
  102.                 gfx.setFont(this.font);
  103.                 gfx.setColor(super.getBackground());
  104.                 gfx.fillRect(0, 0, this.width, this.height);
  105.                 gfx.setColor(super.getForeground());
  106.                 gfx.drawString(this.text, this.width - this.offset, this.baseline);
  107.                 g.drawImage(this.image, 0, 0, null);
  108.  
  109.                 // move
  110.                 this.offset += this.step;
  111.                 if (this.offset >= this.twidth + this.width) 
  112.                         this.offset = 0;
  113.         } 
  114.  
  115.         public boolean mouseDown(Event evt, int x, int y) {
  116.  
  117.                 int off = (-(this.width - this.offset)) + x;
  118.                 if (off < 0 || off > this.twidth)
  119.                         return true;
  120.                 int entry = 0;
  121.                 int begin = 0;
  122.                 for (int i = 0; i < this.url_count; i++)
  123.                         if (off >= begin && off < this.url_off[i]) {
  124.                                 entry = i; 
  125.                                 begin = this.url_off[i];
  126.                         }
  127.                 if (url[entry] != null) {
  128.                         try { 
  129.                                 super.getAppletContext().showDocument(new URL(url[entry]));
  130.                         } catch(MalformedURLException e) { 
  131.                                 System.out.println("Invalid address " + url[entry]);
  132.                         }
  133.                         if (url[entry].substring(0,6).compareTo("mailto") != 0)
  134.                                 this.stop();
  135.                 }
  136.                 return true;
  137.         }
  138.  
  139.         public void run() {
  140.                 while(this.thread != null) {
  141.                         try { Thread.sleep(this.delay); } 
  142.                         catch (InterruptedException e) { }
  143.                         super.repaint();
  144.                 }
  145.         }
  146.  
  147.         public void start() {
  148.                 if (this.thread == null) {
  149.                         this.thread = new Thread(this);
  150.                         this.thread.start();
  151.                 }
  152.         }
  153.  
  154.         public void stop() {
  155.                 if (this.thread != null) {
  156.                         this.thread.stop();
  157.                         this.thread = null;
  158.                 }
  159.         }
  160.  
  161.         public String getAppletInfo() {
  162.                 return "yet another TickerTape.java";
  163.         }
  164.  
  165.         public String[][] getParameterInfo() {
  166.                 String[][] i = {
  167.                         {"font",        "string",       "Font (def TimesRoman)" },
  168.                         {"size",        "int",          "Font Size (def 18)" },
  169.                         {"delay",       "int",          "Scroll delay in ms (def 100)" },
  170.                         {"step",        "int",          "Scroll pixel step (def 3)" },
  171.                         {"count",       "int",          "Number of text messages (def 10)" },
  172.                         {"text0",       "string",       "Text message 0" },
  173.                         {"textn",       "string",       "Text message #n" },
  174.                         {"url0",        "string",       "URL 0 associated with text0" },
  175.                         {"urln",        "string",       "URL #n associated with textn" }
  176.                 };
  177.  
  178.                 return i;
  179.         }
  180. }
  181.  
  182.  
  183.